<template>
  <div class="add">
    <el-card>
      <el-icon><Tickets /></el-icon>数据列表
      <div class="btn">
        <el-button @click="add">添加</el-button>
      </div>
    </el-card>
  </div>
  <el-table
    :data="list"
    border
    table-layout="auto"
    cell-class-name="align-c"
    header-row-class-name="align-c"
  >
    <el-table-column label="编号" prop="id" />
    <el-table-column label="秒杀时间段名称" prop="name" />
    <el-table-column label="每日开始时间" prop="startTime">
      <template #default="{ row }">
        <span>{{ dateFormat(row.startTime) }}</span></template
      >
    </el-table-column>
    <el-table-column label="每日结束时间" prop="endTime">
      <template #default="{ row }">
        <span>{{ dateFormat(row.endTime) }}</span></template
      ></el-table-column
    >
    <el-table-column label="启用" prop="loginTime">
      <template #default="{ row }">
        <el-switch
          v-model="row.status"
          :active-value="1"
          :inactive-value="0"
          @change="updateStatus(row)"
        />
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="{ row }">
        <el-button text type="primary" @click="edit(row)">编辑</el-button>
        <el-button text type="primary" @click="del(row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <editFlashSession
    :show="showDialog"
    @hide="closeDialog"
    :id="editId"
    :name="name"
    :startTime="startTime"
    :endTime="endTime"
    :status="status"
  ></editFlashSession>
</template>

<script setup lang="ts">
import useTable from "@/hooks/useTable";
import { dateFormat } from "@/utils/index";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref } from "vue";
import ReturnReason from "../oms/returnReason.vue";
import {
  deleteFlashSessionApi,
  getFlashSessionApi,
  updateFlashSessionStatusApi,
} from "./api";
import editFlashSession from "./editFlashSession.vue";
const showDialog = ref(false);
const closeDialog = (isReload?: boolean) => {
  showDialog.value = false;
  if (isReload) searchParamsFn();
};

const editId = ref(0);
const name = ref("");
const startTime = ref("");
const endTime = ref("");
const status = ref(0);
const form = reactive({});
const {
  list,
  pageNum,
  total,
  pageChangeHandle,
  pageSizeChangeHandle,
  pageSize,
  searchParamsFn,
} = useTable(getFlashSessionApi, form);
// 切换状态
const updateStatus = (row: IFlashSession) => {
  updateFlashSessionStatusApi(row.id!, row.status)
    .then((res) => {
      if (res.code === 200) {
        // console.log(res);
        ElMessage.success("修改成功");
      } else {
        row.status = row.status === 1 ? 0 : 1;
      }
    })
    .catch(() => {
      row.status = row.status === 1 ? 0 : 1;
    });
};
// 添加
const add = () => {
  showDialog.value = true;
};
// 编辑
const edit = (row: IFlashSession) => {
  showDialog.value = true;
  editId.value = row.id;
  name.value = row.name;
  startTime.value = row.startTime;
  endTime.value = row.endTime;
  status.value = row.status;
};
// 删除
const del = (id: number) => {
  ElMessageBox.confirm("确定删除吗？", "删除", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(() => {
      deleteFlashSessionApi(id).then((res) => {
        if (res.code === 200) {
          ElMessage.success("删除成功");
          searchParamsFn();
        }
      });
    })
    .catch(() => {});
};
</script>

<style lang="less" scoped>
.add {
  margin: 20px 0;
  :deep(.el-card__body) {
    display: flex;
    position: relative;
    align-items: center;
  }
  .btn {
    position: absolute;
    right: 9px;
  }
}
:deep(.align-c) {
  .cell {
    text-align: center !important;
    font-size: 12px;
  }
}
</style>
